package com.example.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sms.entity.Course;
import com.example.sms.entity.Student;
import com.example.sms.entity.dto.LoginDTO;
import com.example.sms.entity.dto.RegisterDTO;
import com.example.sms.exception.SimpleException;
import com.example.sms.mapper.CourseMapper;
import com.example.sms.mapper.StudentMapper;
import com.example.sms.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Student login(LoginDTO student) {
        if (StringUtils.isBlank(student.getUsername())){
            throw new SimpleException("用户名不能为空");
        } else if (StringUtils.isBlank(student.getPassword())) {
            throw new SimpleException("密码不能为空");
        }
//        Student student1 = studentMapper.selectByUsernameAndPassword(student.getUsername(), student.getPassword());
        Student student1 = getOne(new QueryWrapper<Student>().lambda()
                .eq(Student::getUsername,student.getUsername())
                .eq(Student::getPassword,student.getPassword()));
        if (student1 == null){
            throw new SimpleException("用户名或密码错误");
        }else {
            return student1;
        }
    }

    @Override
    public Integer register(RegisterDTO registerDTO) {
        return studentMapper.insert(registerDTO);
    }


    @Override
    public List<Course> listSelfCourse(Integer id) {
        return studentMapper.listSelfCourse(id);
    }



}
